package com.zmm.myyimixian.ui.activity.main.home;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bumptech.glide.Glide;
import com.zmm.myyimixian.R;
import com.zmm.myyimixian.base.BaseActivity;
import com.zmm.myyimixian.bean.HomeGoods;
import com.zmm.myyimixian.config.ConfigPath;
import com.zmm.myyimixian.util.UIManager;

import java.lang.ref.WeakReference;
import java.util.List;

public class ListViewActivity extends BaseActivity implements View.OnClickListener {

    private String url;
    private TextView xl_name_tv;
    private TextView xl_fname_tv;
    private TextView zhrmb_tv;
    private TextView yjrmb_tv;
    private TextView td_tv;
    private TextView drjg_tv;
    private TextView tgsm_tv;
    private TextView drtg_tv;
    private ImageView tpsbf_iv;
    private ImageView tpxbf_iv;
    private ConvenientBanner convenientBanner;
    private MyHandler handler = new MyHandler(this);

    @Override
    protected void initView() {
        initData();
        //返回
        findViewById(R.id.fanhui_iv).setOnClickListener(this);
        //关闭当前页
        findViewById(R.id.gb_iv).setOnClickListener(this);
        //标题
        xl_name_tv = (TextView) findViewById(R.id.xl_name_tv);
        //副标题
        xl_fname_tv = (TextView) findViewById(R.id.xl_fname_tv);
        //折后价格
        zhrmb_tv = (TextView) findViewById(R.id.zhrmb_tv);
        //原价
        yjrmb_tv = (TextView) findViewById(R.id.yjrmb_tv);
        //特点
        td_tv = (TextView) findViewById(R.id.td_tv);
        //单人价格
        drjg_tv = (TextView) findViewById(R.id.drjg_tv);
        //玩法
        findViewById(R.id.wf_tv).setOnClickListener(this);
        //团购说明
        tgsm_tv = (TextView) findViewById(R.id.tgsm_tv);
        //团购价格
        drtg_tv = (TextView) findViewById(R.id.drtg_tv);
        //图片上部分
        tpsbf_iv = (ImageView) findViewById(R.id.tpsbf_iv);
        //图片下部分
        tpxbf_iv = (ImageView) findViewById(R.id.tpxbf_iv);
        //返回首页
        findViewById(R.id.fhsy_iv).setOnClickListener(this);
        //单人
        findViewById(R.id.drjg_ll).setOnClickListener(this);
        //多人
        findViewById(R.id.drtg_ll).setOnClickListener(this);
        //轮播
        convenientBanner = (ConvenientBanner) findViewById(R.id.convenientBanner);
        new MyListViewActivityThread(handler, url).start();
    }

    //点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fanhui_iv:
            case R.id.gb_iv:
                finish();
                break;
            case R.id.wf_tv:
                Toast.makeText(this, "团购步骤", Toast.LENGTH_SHORT).show();
                break;
            case R.id.fhsy_iv:
                UIManager.startMain(this);
                break;
            case R.id.drjg_ll:
                Toast.makeText(this, "不支持单人购买", Toast.LENGTH_SHORT).show();
                break;
            case R.id.drtg_ll:
                UIManager.startNewAddressActivity(this);
                break;
        }
    }

    //接收另一个页面传来的数据
    private void initData() {
        Intent intent = getIntent();
        url = intent.getStringExtra("url");
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_list_view;
    }

    //实现图片轮播
    private void initData(List<String> img_url) {

        convenientBanner.setPages(new CBViewHolderCreator<ListViewActivity.ViewPagerImage>() {
            @Override
            public ListViewActivity.ViewPagerImage createHolder() {
                return new ListViewActivity.ViewPagerImage();
            }
        }, img_url).setPageIndicator(new int[]{R.mipmap.order_list_orange_ball_invalid, R.mipmap.order_list_orange_ball})
                .setPageIndicatorAlign(
                        ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL);
    }


    public class ViewPagerImage implements Holder<String> {
        private ImageView imageView;

        @Override
        public View createView(Context context) {
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            return imageView;
        }

        @Override
        public void UpdateUI(Context context, final int position, String data) {
            Glide.with(ListViewActivity.this).load(ConfigPath.getLunBoPath(data)).placeholder(R.mipmap.icon_home_item_default).into(imageView);
        }
    }

    //重写 onResume onPause 设置自动播放
    @Override
    public void onResume() {
        super.onResume();
        //开始自动翻页
        convenientBanner.startTurning(2000);
    }

    @Override
    public void onPause() {
        super.onPause();
        //停止自动翻页
        convenientBanner.stopTurning();
    }

    //获取网上下载的数据
    private class MyHandler extends Handler {

        private WeakReference<ListViewActivity> reference = null;

        public MyHandler(ListViewActivity listViewActivity) {
            reference = new WeakReference<>(listViewActivity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            ListViewActivity listViewActivity = reference.get();
            if (listViewActivity != null) {
                if (msg.what == 1) {
                    HomeGoods goods = (HomeGoods) msg.obj;
                    HomeGoods.GoodDetailBean.GoodBean good = goods.getGood_detail().getGood();
                    List<String> img_url = good.getImg_url();
                    initData(img_url);
                    initData(good);
                }
            }
        }
    }

    private void initData(HomeGoods.GoodDetailBean.GoodBean good) {
        xl_name_tv.setText(good.getTitle());
        xl_fname_tv.setText(good.getUnit_desc() + "  已售：" + good.getShould_buy_count() + "份");
        String unit_price = good.getUnit_price();
        String unit_old_price = good.getUnit_old_price();
        zhrmb_tv.setText(unit_price);
        yjrmb_tv.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        yjrmb_tv.setText(unit_old_price);
        List<HomeGoods.GoodDetailBean.GoodBean.FunctionNewImgurlBean> function_new_imgurl = good.getFunction_new_imgurl();
        if (function_new_imgurl.size() > 1) {
            tpxbf_iv.setVisibility(View.VISIBLE);
            Glide.with(this).load(ConfigPath.getLunBoPath(function_new_imgurl.get(0).getUrl())).placeholder(R.mipmap.icon_home_item_default).into(tpsbf_iv);
            Glide.with(this).load(ConfigPath.getLunBoPath(function_new_imgurl.get(1).getUrl())).placeholder(R.mipmap.icon_home_item_default).into(tpxbf_iv);
        } else {
            tpxbf_iv.setVisibility(View.GONE);
            Glide.with(this).load(ConfigPath.getLunBoPath(function_new_imgurl.get(0).getUrl())).placeholder(R.mipmap.icon_home_item_default).into(tpsbf_iv);
        }
        drjg_tv.setText(unit_old_price);
        drtg_tv.setText(unit_price);
    }
}
